/*
Account_To_AgencyTeam_PLAS
Version 1.0
Description: Creates Agency_Team__c records under a Account on insertion of Account record and 
on updation of Account record if any or all of fields: OwnerID, CL_Agency_Specialist__c, PLAS__c,
PL_Agency_Specialist__c is changed, also deletes old Agency_Team__c records under an account.
Author: Pratibh Prakash
Created Date: 5 Mar-2009
*/
public class Account_To_AgencyTeam_PLAS {
	/*
	insertAgencyTeam()
	@param accounts: contains list of Account records as sent by trigger(Trigger filters the records).
	@param hrIDs: contains Id of all HR__c records having same User__c value as in any of user type fields in Account record.
	Description: Creates creates Agency_Team__c for Account records.
	*/
	public static void insertAgencyTeam(Account[] accounts, Set<Id> hrIds, Account[] oldAccounts){
		
		System.debug('@@@@@@@@@@@@@@@@@@@'+hrIds.size());
		Map<Id,User> hrIdUserMap = new Map<Id,User>([select Name from User where Id in:hrIds]);
		System.debug('!!!!!!!!!!!!!!'+hrIdUserMap.size());
		Map<ID, HR__c> hrMap = new Map<ID, HR__c>();
		map<String, Agency_Team__c> agtInsert = new map<String, Agency_Team__c>();//Map for storing Agency_Team__c records objects which are to be inserted.
        for(HR__c hr:[Select User__c From HR__c Where User__c In :hrIds]){
            hrMap.put(hr.User__c, hr); 
        }
        System.debug('%%%%%%%%%%%%%%%%%%%%%%%'+hrMap.size());
        
        for(Integer i=0; i<accounts.size(); i++){
        	System.debug('^^^^^^^^^^^^'+accounts.size());
        	String  hrManId  = null;
            String  hrPLASId  = null;
            String  hrCLASId = null;
            String  hrPLTMId = null;
            HR__c curHRMan = hrMap.get(accounts[i].OwnerId);
                        
            if(curHRMan != null) hrManId = curHRMan.Id;
            if(accounts[i].PLAS__c != null){
                HR__c curHRPLAS = hrMap.get(accounts[i].PLAS__c);
                if(curHRPLAS != null) hrPLASId = curHRPLAS.Id;
            }
            if(accounts[i].CL_Agency_Specialist__c != null){
                HR__c curHRPLAS = hrMap.get(accounts[i].CL_Agency_Specialist__c);
                if(curHRPLAS != null) hrCLASId = curHRPLAS.Id;
            }
            if(accounts[i].PL_Agency_Specialist__c != null){
                HR__c curHRPLAS = hrMap.get(accounts[i].PL_Agency_Specialist__c);
                if(curHRPLAS != null) hrPLTMId = curHRPLAS.Id;
            }
            
            //Generating new Agency_Team__c objects which are to be inserted.
            if(oldAccounts.size()<1){
            	Agency_Team__c currAGT = new Agency_Team__c(
                        	Agency__c           = accounts[i].Id,
                        	Role__c             = 'CL Territory Manager',
                        
                        	Team_Member_User__c = accounts[i].OwnerId,
                        	Team_Member__c = ((User)hrIdUserMap.get(accounts[i].OwnerId)).Name,  
                    	    HR_Name__c = hrManId 
                	    );
            	agtInsert.put(accounts[i].Id+'|'+accounts[i].OwnerId,currAGT);
            }
            else{
            	if(oldAccounts != null){
            		if(oldAccounts.size()>0){
            			if(accounts[i].OwnerId != oldAccounts[i].OwnerId){
            				Agency_Team__c currAGT = new Agency_Team__c(
                        		Agency__c           = accounts[i].Id,
                        		Role__c             = 'CL Territory Manager',
	                        	Team_Member_User__c = accounts[i].OwnerId,
    	                    	Team_Member__c = ((User)hrIdUserMap.get(accounts[i].OwnerId)).Name,  
        	            	    HR_Name__c = hrManId 
            	    	    );
            				agtInsert.put(accounts[i].Id+'|'+accounts[i].OwnerId,currAGT);
            			}
            		}
            	}
            }
            if(accounts[i].PLAS__c != null){
            	if(oldAccounts.size()<1 || accounts[i].PLAS__c != oldAccounts[i].PLAS__c){
	            	Agency_Team__c currAGTPLAS = new Agency_Team__c(
    	            Agency__c           = accounts[i].Id,
        	        Role__c             = 'PL Territory Manager',
	                Team_Member_User__c = accounts[i].PLAS__c,
    	            Team_Member__c = ((User)hrIdUserMap.get(accounts[i].PLAS__c)).Name,                              
        	        HR_Name__c = hrPLASId
            	    );
            	    agtInsert.put(accounts[i].Id+'|'+accounts[i].PLAS__c,currAGTPLAS);
            	}
            }
            
            if(accounts[i].CL_Agency_Specialist__c != null){
            	if(oldAccounts.size()<1 ||accounts[i].CL_Agency_Specialist__c != oldAccounts[i].CL_Agency_Specialist__c){
	            	Agency_Team__c currAGTCLAS = new Agency_Team__c(
    	            Agency__c = accounts[i].Id,
        	        Role__c = 'CL Agency Specialist',
					Team_Member_User__c = accounts[i].CL_Agency_Specialist__c,
					Team_Member__c = ((User)hrIdUserMap.get(accounts[i].CL_Agency_Specialist__c)).Name,
        	        HR_Name__c = hrCLASId
            	    );
            	    agtInsert.put(accounts[i].Id+'|'+accounts[i].CL_Agency_Specialist__c,currAGTCLAS);
            	}
			}
			
			if(accounts[i].PL_Agency_Specialist__c != null){
				if(oldAccounts.size()<1 ||accounts[i].PL_Agency_Specialist__c != oldAccounts[i].PL_Agency_Specialist__c){
					Agency_Team__c currAGTPLASPL = new Agency_Team__c(
	                Agency__c = accounts[i].Id,
    	            Role__c = 'PL Agency Specialist',
					Team_Member_User__c = accounts[i].PL_Agency_Specialist__c,
    	            Team_Member__c = ((User)hrIdUserMap.get(accounts[i].PL_Agency_Specialist__c)).Name,
        	        HR_Name__c = hrPLTMId
            	    );
            	    agtInsert.put(accounts[i].Id+'|'+accounts[i].PL_Agency_Specialist__c,currAGTPLASPL);
				}
			}
        }
         if(agtInsert != null)
            if(agtInsert.size() > 0)insert (agtInsert.values());
	}
}